#include "cgic.h"
#include <glib.h>
#include "MsgUtils.h"
#include "global.h"
int cgiMain(){
    //获取用户请求并解析
    char *gatewayIp[128];
    char *location[128];
    cgiFormString("gatewayIp",gatewayIp,128);
    cgiFormString("location",location,128);
    //写入到指定配置文件
    //获取配置文件路径
    GKeyFile *configIniPath = g_key_file_new();
    if (!g_key_file_load_from_file(configIniPath,"./config.ini",G_KEY_FILE_NONE,NULL))
    {
        g_warning("load config.ini error!!!");
        return -1;
    }
    gchar *path = g_key_file_get_string(configIniPath,"config","devinfo_path",NULL);
    //释放
    g_key_file_free(configIniPath);
    //写入
    GKeyFile *configPath = g_key_file_new();
    g_key_file_set_string(configPath,"config","gatewayIp",gatewayIp);
    g_key_file_set_string(configPath,"config","location",location);
    g_key_file_save_to_file(configPath,path,NULL);
    //释放
    g_key_file_free(configPath);
    //通知心跳进程，读取数据发请求
    //获取队列
    int mqId = getMsgQueue(PATHNAME_BOA_MAIN,PROCESS_ID_BOA_MAIN);
    if (mqId == -1)
    {
        perror("create mqId failed");
        return -1;
    }
    Message msg = {
        .type = 1,
        .text = "data1"
    };
    int sendSize = sendMsg(mqId,&msg,strlen(msg.text) + 1);
    if (sendSize == -1)
    {
        perror("send msg failed");
        return -1;
    }
    //响应
    cgiHeaderContentType("text/html");
	/* Top of the page */
	fprintf(cgiOut, "<HTML><HEAD> <meta charset='UTF-8'>\n");
	fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY><H1>success</H1>\n");
    fprintf(cgiOut, "</BODY></HTML>\n");
    return 0;
}